/**
 * 
 */
package com.epart.widget.module.component;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.epart.widget.module.login.LoginService;
import com.epart.widget.util.StringUtil;

/**
 * 위젯 컴포넌트
 * 
 * @author cwsong
 * @version 2011. 3. 10.
 */
public class WcmsWidgetLogin extends AbstractWcmsWidgetJPanel {

	private JTextField memberId;
	private JPasswordField memberPassword;
	private LoginService loginService;
	private WcmsWidget wcmsWidget;

	/**
	 * @param loginService
	 *            the loginService to set
	 */
	public void setLoginService(LoginService loginService) {
		this.loginService = loginService;
	}

	/**
	 * @param wcmsWidget
	 *            the wcmsWidget to set
	 */
	public void setWcmsWidget(WcmsWidget wcmsWidget) {
		this.wcmsWidget = wcmsWidget;
	}

	/**
	 * 생성자
	 */
	public WcmsWidgetLogin() {
		super();
		setBackground(Color.WHITE);
		setLayout(null);

		JLabel labelName = new JLabel("아이디");
		labelName.setBounds(52, 38, 36, 15);
		add(labelName);

		memberId = new JTextField();
		memberId.setBounds(113, 35, 116, 21);
		add(memberId);
		memberId.setColumns(10);

		JLabel labelPassword = new JLabel("비밀번호");
		labelPassword.setBounds(52, 69, 48, 15);
		add(labelPassword);

		final JButton btnLogin = new JButton("로그인");
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (StringUtil.isEmpty(memberId.getText())) {
					JOptionPane.showMessageDialog(wcmsWidget, "아이디를 입력해주세요.", DEFAULT_LOGIN_TITLE, JOptionPane.INFORMATION_MESSAGE);
					return;
				}
				if (StringUtil.isEmpty(new String(memberPassword.getPassword()))) {
					JOptionPane.showMessageDialog(wcmsWidget, "비밀번호를 입력해주세요.", DEFAULT_LOGIN_TITLE, JOptionPane.INFORMATION_MESSAGE);
					return;
				}
				if (loginService.isIdExistAndPasswordCorrent(memberId.getText(), new String(memberPassword.getPassword())) <= 3) {
					// JOptionPane.showMessageDialog(topJFrame, "로그인 성공",
					// DEFAULT_LOGIN_TITLE, JOptionPane.INFORMATION_MESSAGE);
					wcmsWidget.loadWcmsWidgetMain();
				} else {
					JOptionPane.showMessageDialog(wcmsWidget, "입력하신 정보가 정확하지 않습니다.", DEFAULT_LOGIN_TITLE, JOptionPane.INFORMATION_MESSAGE);
					return;
				}
			}
		});
		btnLogin.setBounds(72, 117, 69, 23);
		add(btnLogin);

		JButton btnCancel = new JButton("취소");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		btnCancel.setBounds(153, 117, 57, 23);
		add(btnCancel);

		memberPassword = new JPasswordField();
		memberPassword.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					btnLogin.doClick();
				}
			}
		});
		memberPassword.setBounds(113, 66, 116, 21);
		add(memberPassword);
	}
}
